package com.huitone.smspfm.core.filter;

import java.util.List;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

import com.huitone.smspfm.logger.ExtLogger;

import com.bin.common.utils.StringUtils;
import com.huitone.smspfm.core.config.AppStaticConfigs;
import com.huitone.smspfm.core.constant.DealResult;
import com.huitone.smspfm.model.SmsEntityForMt;

/**   
* @Title SensWorldFilterTask.java 
* @Package com.huitone.smspfm.core.task 
* @Description TODO(用一句话描述该文件做什么) 
* @author ybliang   
* @date 2017年6月17日 下午5:04:20 
* @version V1.0   
*/
public class SensWorldFilterTask extends Thread {
	
	private ExtLogger logger = ExtLogger.create(SensWorldFilterTask.class);
	
	private String moduleName = "敏感词过滤模块";
	
	//private byte[] moduleNameB = AppStaticConfigs.stringRedisSerializer.serialize(moduleName); 
	
	private List<SmsEntityForMt> entities = null;
	
	private CyclicBarrier barrier = null;
	
	public SensWorldFilterTask(List<SmsEntityForMt> entities, CyclicBarrier barrier) {
		this.entities = entities;
		this.barrier = barrier;
	}
	
	@Override
	public void run() {
		
		if (entities == null) return;
		
		long start = System.currentTimeMillis();
		
		for (int i = 0 ; i < entities.size() ; i ++) {
			
			SmsEntityForMt entity = entities.get(i);
			
			String content = StringUtils.toString(entity.getContent());
			
			boolean containSens = false;
			
			for (String sens:AppStaticConfigs.configSensitiveSet) {
				if (StringUtils.contains(content, sens)) {
					containSens = true;
				}
			}
			
			if (containSens) {
				
				entity.setDealResultCodeMap(moduleName, DealResult.RESULT_3);
				entity.setInterceptedCode(DealResult.RESULT_3);
				entity.setInterceptedDesc(DealResult.RESULT_3_DESC);
				entity.setIntercepted(true);
				
				logger.debug("[序列号:用户号码][" + entity.getSerialno() + ":" + entity.getPhoneNumber() + "]内容包含敏感词。");
				
			} else {
				
				entity.setDealResultCodeMap(moduleName, DealResult.RESULT0);
			}
		}
		
		long end = System.currentTimeMillis();
		
		logger.debug("敏感词过滤执行完毕,共耗时:" + (end - start) + "毫秒");
		
		try {
			barrier.await();
		} catch (InterruptedException | BrokenBarrierException e) {
			e.printStackTrace();
		}
	}
}
